package service

import (
	"singo/model"
	"singo/serializer"
	// "github.com/segmentio/ksuid"
	// "github.com/rs/xid"
)

// UserResetPasswordService 管理用户密码服务
type UserResetPasswordService struct {
}

// ResetPassword 用户密码服务
func (service *UserResetPasswordService) ResetPassword(id string) serializer.Response {

	user := model.User{
		Password: "qwe123",
	}

	// 加密密码
	if err := user.SetPassword("qwe123"); err != nil {
		return serializer.Err(
			serializer.CodeEncryptError,
			"密码加密失败",
			err,
		)
	}

	// 更新用户
	if err := model.DB.Model(&user).Where("uid = ?", id).Updates(&user).Error; err != nil {
		return serializer.ParamErr("更新失败", err)
	}

	return serializer.Response{
		Data:   true,
		Msg:    "成功",
		Status: 0,
	}
}

type UserChangePasswordService struct {
	// Password        string `form:"password" json:"password" binding:"required,min=6,max=40"`
	NewPassword     string `form:"new_password" json:"new_password" binding:"required,min=6,max=40"`
	PasswordConfirm string `form:"password_confirm" json:"password_confirm" binding:"required,min=6,max=40"`
}

// ResetPassword 用户密码服务
func (service *UserChangePasswordService) ChangePassword(id string) serializer.Response {

	user := model.User{
		Password: service.NewPassword,
	}

	// 加密密码
	if err := user.SetPassword(service.NewPassword); err != nil {
		return serializer.Err(
			serializer.CodeEncryptError,
			"密码加密失败",
			err,
		)
	}

	if service.PasswordConfirm != service.NewPassword {
		return serializer.Response{
			Status: 40001,
			Msg:    "两次输入的密码不相同",
		}
	}

	// 更新用户
	if err := model.DB.Model(&user).Where("uid = ?", id).Updates(&user).Error; err != nil {
		return serializer.ParamErr("更新失败", err)
	}

	return serializer.Response{
		Data:   true,
		Msg:    "成功",
		Status: 0,
	}
}
